<template>
  <div class="userManagement-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-row>
          <el-col :span="8"><MyRecord></MyRecord></el-col>
          <el-col :span="8">
            <UploadRecord :my-refresh="fetchData"></UploadRecord>
          </el-col>
          <el-col :span="8">
            <el-button
              icon="el-icon-delete"
              type="danger"
              @click="handleDelete"
            >
              批量删除
            </el-button>
          </el-col>
        </el-row>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.name"
              placeholder="请输入录音名"
              clearable
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      :height="height"
      :element-loading-text="elementLoadingText"
      @selection-change="setSelectRows"
    >
      <el-table-column show-overflow-tooltip type="selection"></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="name"
        label="音频名称"
      ></el-table-column>
      <el-table-column show-overflow-tooltip prop="size" label="音频大小">
        <template slot-scope="scope">{{ scope.row.size }}M</template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="duration" label="视频时长">
        <template slot-scope="scope">{{ scope.row.duration }}s</template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createBy"
        label="上传者"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="createTime"
        label="上传时间"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="200">
        <template #default="{ row }">
          <el-row>
            <el-col :span="6">
              <RecordPlay :audiourl="row.fileUrl"></RecordPlay>
            </el-col>
            <el-col :span="6">
              <el-button type="text" @click="mergeRecord(row)">
                发起合成
              </el-button>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNo"
      :page-size="queryForm.pageSize"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
  </div>
</template>

<script>
  import { recordDelete, recordList } from '@/api/record'
  import MyRecord from './components/myRecord'
  import UploadRecord from './components/uploadRecord'
  import RecordPlay from './components/recordPlay'
  export default {
    name: 'Videos',
    components: { MyRecord, UploadRecord, RecordPlay },
    data() {
      return {
        list: [],
        listLoading: true,
        background: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          name: '',
          pageNo: 1,
          pageSize: 10,
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      mergeRecord(val) {
        this.$router.push({ path: '/mergeRecord', query: { id: val.id } })
      },
      handleDelete() {
        if (this.selectRows.length) {
          const ids_join = this.selectRows.map((item) => item.id).join()
          let paykey = {
            ids: [],
          }
          let temp = ''
          for (let i = 0; i < ids_join.length; i++) {
            if (ids_join[i] !== ',') temp = temp + ids_join[i]
            else {
              paykey.ids.push(Number(temp))
              temp = ''
            }
          }
          paykey.ids.push(Number(temp))
          this.$baseConfirm('你确定要删除选中项吗', null, async () => {
            const { msg } = await recordDelete(paykey)
            this.$baseMessage('删除成功', 'success')
            await this.fetchData()
          })
        } else {
          this.$baseMessage('未选中任何行', 'error')
          return false
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const query = await recordList(this.queryForm)
        let data = query.data
        let temp = data.data
        for (let i = 0; i < temp.length; i++) {
          temp[i].size = (Number(temp[i].size) / 1024 / 1024).toFixed(2)
        }
        this.list = temp
        this.total = data.totaResults
        setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
